package com.thejoyrun.retrofit.http;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

/**
 * Created by kkmike999 on 2016/05/08.
 */
public class CookieManager implements CookieJar {

	public static final String domain = "thejoyrun.com";

	@Override
	public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
		// 由于悦跑圈仅拼接uid&sid，没必要保存cookies
	}

	@Override
	public List<Cookie> loadForRequest(HttpUrl url) {
		try {
			String sid = "sid";
			int    uid = 1;
			if (uid > 0) {
				String value = URLEncoder.encode(String.format("sid=%s&uid=%s", sid, uid), "utf-8")
				                         .toLowerCase();

				Cookie cookie = new Cookie.Builder().name("ypcookie")
				                                    .value(value)
				                                    .domain(domain)
				                                    .path("/")
				                                    .expiresAt(4070883661000L)
				                                    .build();

				return Collections.singletonList(cookie);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return new ArrayList<>();
	}
}
